package org.easyway.tiles.generic;

//import static org.easyway.tiles.generic.GraphicBoard.tileHeight;
//import static org.easyway.tiles.generic.GraphicBoard.tileWidth;
//import static org.easyway.tiles.generic.GraphicBoard.worldHeight;
//import static org.easyway.tiles.generic.GraphicBoard.worldWidth;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.MediaTracker;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.EventListenerList;

public class GraphicBoard extends JPanel {
	private static final long serialVersionUID = -7193477105237066050L;

	public static int tileHeight = 64;

	public static int tileWidth = 64;

	public static int worldWidth = 20;

	public static int worldHeight = 20;

	public static MediaTracker mt;

	public static CustomImage imageGrid[][];

	protected JScrollPane scroll;

	public GraphicBoard(int ww, int wh, int tw, int th) {
		mt = new MediaTracker(this);
		tileHeight = th;
		tileWidth = tw;
		worldWidth = ww;
		worldHeight = wh;
		// tiles = new CustomTile[worldWidth][worldHeight];
		imageGrid = new CustomImage[worldWidth][worldHeight];
		this.setLayout(null);
		this.setPreferredSize(new Dimension(tileWidth * worldWidth, tileHeight
				* worldHeight));
		this.setSize(new Dimension(700, 500));
		CopierTool.board = this;
		CustomImage.board = this;
		OptimizedSaver.graphicBoard = this;
	}

	public void changeDimension(int ww, int wh, int tw, int th) {
		tileHeight = th;
		tileWidth = tw;
		worldWidth = ww;
		worldHeight = wh;
		// tiles = new CustomTile[worldWidth][worldHeight];
		imageGrid = new CustomImage[worldWidth][worldHeight];

		this.setPreferredSize(new Dimension(tileWidth * worldWidth, tileHeight
				* worldHeight));
		repaint();
	}

	public void setTile(int mx, int my, CustomImage t) {
		int x = Math.min(worldWidth - 1, Math.max(0, mx
				/ (int) (tileWidth / ZoomManager.getZoomFactor())));
		int y = Math.min(worldHeight - 1, Math.max(0, my
				/ (int) (tileHeight / ZoomManager.getZoomFactor())));

		// System.out.println(x + " " + y);
		// if (tiles[x][y] != null)
		// this.remove(tiles[x][y]);
		// tiles[x][y] = new CustomTile(t);
		// tiles[x][y].setBounds(x * tileWidth, y * tileHeight, tileWidth,
		// tileHeight);
		// this.add(tiles[x][y]);
		imageGrid[x][y] = t;
		this.repaint();
	}

	public void update(Graphics g) {
		this.paint(g);
	}

	public void paint(Graphics g) {
		super.paint(g);
		drawGrid(g);
		fireGraphicBoardBPainted(g);
	}

	/**
	 * draws a grid
	 * 
	 * @param g
	 */
	private void drawGrid(java.awt.Graphics g) {
		int sizeTilex = (int) (tileWidth / ZoomManager.getZoomFactor());
		int sizeTiley = (int) (tileHeight / ZoomManager.getZoomFactor());

		int startx = scroll.getHorizontalScrollBar().getValue();

		int starty = scroll.getVerticalScrollBar().getValue();

		int endx = Math.min(scroll.getWidth() + startx, worldWidth * sizeTilex);
		int endy = Math.min(scroll.getHeight() + starty, worldHeight
				* sizeTiley);

		int numx = (scroll.getWidth() / sizeTilex + 2) * sizeTilex + startx;
		int numy = (scroll.getHeight() / sizeTiley + 2) * sizeTiley + starty;

		int shiftx = startx % sizeTilex;
		int shifty = starty % sizeTiley;

		for (int y = starty - shifty; y < numy; y += sizeTiley) {
			for (int x = startx - shiftx; x < numx; x += sizeTilex) {
				if (x / sizeTilex < worldWidth && y / sizeTiley < worldHeight)
					if (imageGrid[x / sizeTilex][y / sizeTiley] != null)
						g.drawImage(
								imageGrid[x / sizeTilex][y / sizeTiley].img,// .image,
								x, y, sizeTilex, sizeTiley, this);
			}
		}

		for (int x = startx - shiftx; x < numx; x += sizeTilex) {
			g.drawLine(x, starty, x, endy);
		}

		for (int y = starty - shifty; y < numy; y += sizeTiley) {
			g.drawLine(startx, y, endx, y);
		}

	}

	public JScrollPane getScroll() {
		return scroll;
	}

	public void setScroll(JScrollPane scroll) {
		this.scroll = scroll;
	}

	// Listener methods
	EventListenerList GraphicBoardListeners = new EventListenerList();

	public void addGraphicBoardListener(GraphicBoardListener listener) {
		GraphicBoardListeners.add(GraphicBoardListener.class, listener);
	}

	public void removeGraphicBoardListener(GraphicBoardListener listener) {
		GraphicBoardListeners.remove(GraphicBoardListener.class, listener);
	}

	protected void fireGraphicBoardBPainted(Graphics g) {
		Object[] listeners = GraphicBoardListeners.getListenerList();

		// loop through each listener and pass on the event if needed
		int numListeners = listeners.length;
		for (int i = 0; i < numListeners; i += 2) {
			if (listeners[i] == GraphicBoardListener.class) {
				// pass the event to the listeners event dispatch method
				((GraphicBoardListener) listeners[i + 1])
						.GraphicBoardPainted(g);
			}
		}
	}

}
